package com.wondertek.applet.controller.business;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wondertek.common.base.Result;
import com.wondertek.common.log.AutoLogUtil;
import com.wondertek.service.dto.app.req.AppAccessoryPageReq;
import com.wondertek.service.dto.business.req.AccessoryPageReq;
import com.wondertek.service.dto.business.res.AccessoryPageRes;
import com.wondertek.service.dto.business.res.SiteDeviceAccessoryRes;
import com.wondertek.service.dto.sys.req.BaseUpdateReq;
import com.wondertek.service.entity.business.AccessoryPo;
import com.wondertek.service.entity.business.DevicePo;
import com.wondertek.service.entity.business.IpcAccessoryPo;
import com.wondertek.service.entity.business.RoomPo;
import com.wondertek.service.sdk.iotdh.dto.req.SetSmokeAccessoryInfoReq;
import com.wondertek.service.sdk.iotdh.dto.res.GetAccessoryInfoRes;
import com.wondertek.service.sdk.iotdh.dto.res.alarmBell.SetODAlarmBellAccessoryInfoReq;
import com.wondertek.service.sdk.iotdh.dto.res.doorDetector.SetDoorDetectorAccessoryInfoReq;
import com.wondertek.service.sdk.iotdh.dto.res.doorDetectorPlus.SetDoorDetectorPlusAccessoryInfoReq;
import com.wondertek.service.sdk.iotdh.dto.res.keyboard.SetKeyboardAccessoryInfoReq;
import com.wondertek.service.sdk.iotdh.dto.res.pir.SetPIRAccessoryInfoReq;
import com.wondertek.service.sdk.iotdh.service.DhDeviceService;
import com.wondertek.service.service.business.AccessoryService;
import com.wondertek.service.service.business.DeviceService;
import com.wondertek.service.service.business.IpcAccessoryService;
import com.wondertek.service.service.business.RoomService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 配件管理
 * @author: qinjianhui
 * @create: 2024-07-31 14:06
 **/

@RestController
@RequestMapping("/app/business/accessory/")
@Tag(name = "配件管理")
@CrossOrigin
public class AccessoryController {

    @Resource
    private AccessoryService accessoryService;

    @Resource
    private DhDeviceService dhDeviceService;

    @Resource
    private RoomService roomService;
    @Resource
    private DeviceService deviceService;

    @Resource
    private IpcAccessoryService ipcAccessoryService;

    @PostMapping("/getStreamUrlByAccessory")
    @Operation(summary = "配件获取视频")
    public Result<String> getStreamUrlByAccessory(@RequestParam("businessType") String businessType,
                                                  @RequestParam("snCode") String snCode,
                                                  @RequestParam("startTime") String startTime,
                                                  @RequestParam("endTime") String endTime){
        Result result = new Result<>();
        result.setData(ipcAccessoryService.getStreamUrlByAccessory(snCode,startTime,endTime,businessType));
        return result;
    }

    @PostMapping("/getBindInfo")
    @Operation(summary = "获取绑定信息")
    public Result<List<IpcAccessoryPo>> getBindInfo(@RequestParam("accessorySn") String accessorySn){
        LambdaQueryWrapper<IpcAccessoryPo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(IpcAccessoryPo::getAccessorySn,accessorySn);
        List<IpcAccessoryPo> list = ipcAccessoryService.list(lambdaQueryWrapper);
        return Result.success(list);
    }

    @PostMapping("/page")
    @Operation(summary = "分页查询")
    public Result<Page<AccessoryPageRes>> page(@RequestBody AccessoryPageReq req){
        return Result.success(accessoryService.webPage(req));
    }

    @PostMapping("/detail")
    @Operation(summary = "详情")
    public Result<GetAccessoryInfoRes> detail(@RequestBody @Validated BaseUpdateReq req){
        return accessoryService.detail(req.getId());
    }

    @PostMapping("/set")
    @Operation(summary = "烟感开关设置")
    public Result<?> setSmokeAccessoryInfo(@RequestBody @Validated SetSmokeAccessoryInfoReq req) {
        AccessoryPo accessoryPo = new AccessoryPo();
        accessoryPo.setAccessoryCode(req.getAccessoryInfos().get(0).getId());
        SiteDeviceAccessoryRes res = deviceService.selectSiteDeviceAccessory(accessoryPo);
        AutoLogUtil.save("sys","log.smoke.switch.set","烟感sn码:"+res.getAccessorySn(),
                res.getSiteName(),res.getDeviceSn(),res.getSiteId(),res.getAccessorySn());
        //获取deviceSn
        AccessoryPo byId = accessoryService.getById(req.getDeviceId());
        if(null==byId){
            return Result.error("配件不存在！");
        }
        RoomPo roomPo = roomService.getById(byId.getRoomId());
        if(null==roomPo){
            return Result.error("区域不存在！");
        }
        DevicePo devicePo = deviceService.getById(roomPo.getDeviceId());
        if(null==devicePo){
            return Result.error("此配件网关不存在！");
        }
        req.setDeviceId(devicePo.getSnCode());
        return dhDeviceService.setSmokeAccessoryInfo(req);
    }

    @PostMapping("/keyboard/set")
    @Operation(summary = "开关设置（无线键盘）")
    public Result<?> setSmokeAccessoryInfo(@RequestBody @Validated SetKeyboardAccessoryInfoReq req) {
        return dhDeviceService.setKeyboardAccessoryInfo(req);
    }

    @PostMapping("/setODAlarmBellAccessoryInfo")
    @Operation(summary = "设置室外警号")
    public Result<?> setODAlarmBellAccessoryInfo(@RequestBody @Validated SetODAlarmBellAccessoryInfoReq req) {
        return dhDeviceService.setODAlarmBellAccessoryInfo(req);
    }

    @PostMapping("/setDoorDetectorAccessoryInfo")
    @Operation(summary = "设置⻔磁信息")
    public Result<?> setDoorDetectorAccessoryInfo(@RequestBody @Validated SetDoorDetectorAccessoryInfoReq req) {
        return dhDeviceService.setDoorDetectorAccessoryInfo(req);
    }

    @PostMapping("/setDoorDetectorPlusAccessoryInfo")
    @Operation(summary = "设置升级⻔磁信息")
    public Result<?> setDoorDetectorPlusAccessoryInfo(@RequestBody @Validated SetDoorDetectorPlusAccessoryInfoReq req) {
        return dhDeviceService.setDoorDetectorPlusAccessoryInfo(req);
    }

    @PostMapping("/setPIRAccessoryInfo")
    @Operation(summary = "设置PIR相机信息")
    public Result<?> setPIRAccessoryInfo(@RequestBody @Validated SetPIRAccessoryInfoReq req) {
        return dhDeviceService.setPIRAccessoryInfo(req);
    }

}

